/* 
 * File:   RTSection.h
 * Author: Jeffrey Geyssens
 *
 * Created on May 13, 2010, 9:07 PM
 *
 * This is a container for RT's used in MAP file.
 * The container is a map. The key is the string corresponding to the RTs id.
 * The data stored in map is actual RT element.
 */

#ifndef _RTSECTION_H
#define	_RTSECTION_H

#include <map>   
#include <string>
#include "ReferenceTerminal.h"


using namespace std;

class RTSection {
public:
    RTSection();
    RTSection(const RTSection& orig);
    virtual ~RTSection();
    void print();
    void buildFile(string filename);
    void buildSpec(string filename);

    void newRT(string s);
    void printRTlist();
    void printRTlist(int entryNo);
    string getRTid(int entryNo);
    void setTable(string RT, string table);
    void setAttrib(string RT, string sym);

private:
    map<string, ReferenceTerminal> section;
    bool exists(string RTid);
    void addRT(string RTid);

};

RTSection::RTSection() {

}

RTSection::RTSection(const RTSection& orig) {
    section = orig.section;
}

RTSection::~RTSection() {

}

void RTSection::printRTlist() {
    int i;
    i = 1;
    map<string, ReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        cout << i << ") " << (*it).first << endl;
        i++;
    }
}
//TODO: If table or attribute is empty cout something special
void RTSection::printRTlist(int entryNo) {
    int i;
    i = 1;
    map<string, ReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        if (i == entryNo) {
            ReferenceTerminal& RT = (*it).second;
            cout << RT << endl;
            cout << "table: " <<  RT.getTable() << endl;
            cout << "attribute: " <<  RT.getAttrib() << endl;
        }
        i++;
    }


}

string RTSection::getRTid(int entryNo) {
    int i;
    i = 1;
    map<string, ReferenceTerminal>::iterator it;
  //  it = section.begin() + entryNo - 1;

 
    for (it = section.begin(); it != section.end();
            ++it) {
        if (i == entryNo) {
            ReferenceTerminal& RT = (*it).second;
            return RT.getId();
        }
        i++;
    }
}

void RTSection::print() {
    cout << ".RTMATCH" << "\n\n";
    map<string, ReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        ReferenceTerminal& d = (*it).second;
        cout << d << endl;


    }
    cout << ".END_RTMATCH" << "\n";
}

void RTSection::buildFile(string filename) {
    //this->print();
    ofstream myfile(filename.c_str(), ios::app);
    myfile << ".RTMATCH" << "\n\n";
    myfile.close();

    multimap<string, ReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        ReferenceTerminal& d = (*it).second;

        myfile.close();
        d.buildFile(filename);
    }

    ofstream xfile(filename.c_str(), ios::app);
    xfile << "\n.END_RTMATCH" << "\n";
    xfile.close();
}

void RTSection::buildSpec(string filename) {
    ofstream myfile(filename.c_str(), ios::app);
    multimap<string, ReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        ReferenceTerminal& r = (*it).second;

        r.buildSpec(filename);
myfile.close();
    }
}

bool RTSection::exists(string RTid) {
    map<string, ReferenceTerminal>::iterator it;
    it = section.find(RTid);
    if (it != section.end())
        return true;
    else
        return false;
}

//TODO: Do away with newRT and newDRT
//TODO: Rename all occurances of RTMATCH and DRTMATCH to RTMAP and DRTMAP

void RTSection::addRT(string RTid) {
    ReferenceTerminal RT;
    RT.setId(RTid);
    section.insert(pair<string, ReferenceTerminal> (RTid, RT));
}

void RTSection::newRT(string s) {
    //TODO:Check format (last 3 todos also for DRT and LT if needed)
    this->addRT(s);

}

void RTSection::setTable(string RT, string table) {
    map<string, ReferenceTerminal>::iterator it;
    it = section.find(RT);
    (*it).second.setTable(table);
}

void RTSection::setAttrib(string RT, string symbol) {
    map<string, ReferenceTerminal>::iterator it;
    it = section.find(RT);
    (*it).second.setAttrib(symbol);

}

#endif	/* _RTSECTION_H */

